﻿#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

void GamePlaying()
{
	int i,j;
	system("cls");
	printf("WELCOME TO SNAKE GAME!!!\n");
	printf("=============================\n");
	printf("Your scores: 69\n");
	for (i = 0; i < 20; i++)
	{
		for (j= 0; j < 40; j++)
		{		
			printf(i==0 || j==0 || i==19 || j==39 ? "*": " ");			
		}
		printf("\n");
	}
	printf("Press X to exit, press R to restart, press B to back to menu\n");
}

void SelectMap()
{
	system("cls");
	printf("WELCOME TO SNAKE GAME!!!\n");
	printf("=============================\n");
	printf("List exist maps:\n");
	printf("1. Snake in field\n"); //default
	printf("2. Snake in bay\n"); //many islands
	printf("3. Snake in maze\n"); 
	printf("4. My map 1\n");
	printf("5. My map 2\n");
	printf("Enter number to select a map, enter 0 to back, enter -1 to exit game\n");
	//after selected map, go to play
}

void CreateCustomMap()
{
	int i,j;
	system("cls");
	printf("WELCOME TO SNAKE GAME!!!\n");
	printf("=============================\n");
	printf(" - Enter map name: My map 1\n");
	printf(" - Enter map size: 15 40\n");	
	printf(" - Map preview:\n");
	//this preview
	for (i = 0; i < 15; i++)
	{
		for (j= 0; j < 40; j++)
		{		
			printf(i==0 || j==0 || i==14 || j==39 ? "*": " ");			
		}
		printf("\n");
	}
	printf(" - Enter map item[1][1] (0 for, 1 for): 1\n");
	//After finish
	printf("Press X to exit, press S to save, press B to back\n");
}

void About()
{
	system("cls");
	printf("WELCOME TO SNAKE GAME!!!\n");
	printf("=============================\n");
	printf(" - Creator: Tong Quang Khanh\n");
	printf(" - Email: a@b.com\n");
	printf(" - More...\n");
	printf(" Thank you to use this game.\n");
	printf("Press X to exit, press B to back\n");
}

void MainMenu()
{
	system("cls");
	printf("WELCOME TO CAR RACING GAME!!!\n");
	printf("=============================\n");	
	printf("1. Start new game\n");	
	printf("2. Resume game\n");	//only show if last user save and exit last play, same with start game screen
	printf("3. Select Map\n");	
	printf("4. Create custom map\n");	
	printf("5. About\n");	
	printf("6. Exit\n");	

}

void ColectAllScreens(void (*SnakeGameScreen[])(), int *length)
{
	SnakeGameScreen[0] = MainMenu;
	SnakeGameScreen[1] = GamePlaying;
	SnakeGameScreen[2] = SelectMap;
	SnakeGameScreen[3] = CreateCustomMap;
	SnakeGameScreen[4] = About;
	*length = 5;
}

void Display()
{
	void (*SnakeGameScreen[20])();
	int i = 0, isQuit = 0, length;

	ColectAllScreens(SnakeGameScreen, &length);
	SnakeGameScreen[0]();

	while(1)
	{
		char key;
		if( kbhit( ) )
		{
			key = getch();
			switch( key )
			{
			case 75:
				i--;
				if(i<0) i=length - 1;
				SnakeGameScreen[i]();
				break;
			case 77:
				i++;
				if(i==length) i=0;
				SnakeGameScreen[i]();
				break;
			case 27:				
				isQuit = 1;
				break;
			}
		}

		if(isQuit)
		{
			break;
		}
	}
}

int main()
{
	Display();	
	return 0;
}